//
//  ImageLabView.m
//  HuaRun2
//
//  Created by admin on 17/8/29.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "ImageLabView.h"
#import "NSString+Extension.h"

@interface ImageLabView ()

@end
@implementation ImageLabView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#pragma mark - life cycle
- (void)dealloc {
    NSLog(@"%@ dealloc", [self class]);
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self viewInit];
        [self layoutInit];
    }
    return self;
}

- (void)viewInit {
    self.backgroundColor = kRandomColor;
    
    [self addSubview:self.iconView];
    [self addSubview:self.nameLab];
//    self.infoStr = @"你想想";
    
    UITapGestureRecognizer * longPressGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)];
    [self addGestureRecognizer:longPressGr];
    
}

- (void)layoutInit {
    weakself(ws);
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws).offset(0);
        make.left.mas_equalTo(ws).offset(0);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(ws.iconView);
        make.left.mas_equalTo(ws.iconView.mas_right).offset(kMargin10);
        make.right.mas_equalTo(ws.mas_right).offset(-0);
    }];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(ws);
        make.bottom.mas_equalTo(ws.nameLab).offset(kMargin10);
    }];

}

- (void)setInfoStr:(NSString *)infoStr {
    _infoStr = infoStr;
    _nameLab.text = infoStr;
}

- (void)setImageSize:(CGSize)imageSize {
    _imageSize = imageSize;

    weakself(ws);
    CGSize size = self.imageSize.height == 0 ? CGSizeMake(15, 15) : self.imageSize;
    
    [self.iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws);
        make.size.mas_equalTo( size);
        make.centerY.mas_equalTo(ws);
    }];
}


#pragma mark - getter
- (UIImageView *)iconView {
    if (!_iconView) {
        self.iconView = [UIImageView new];
        _iconView.image = [UIImage imageNamed:@"电话001"];
    }
    return _iconView;
}

- (UILabel *)nameLab {
    if (!_nameLab) {
        self.nameLab = [UILabel new];
        _nameLab.textAlignment = NSTextAlignmentLeft;
        _nameLab.font = kGeneralFontSize;
        _nameLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
    }
    return _nameLab;
}

- (void)handleTap {
    self.clickBlock ? self.clickBlock(_eventType, _nameLab.text): nil;
}

@end
